package com.quectel.core.module.really.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.really.dao.ReallyCompanyUserDao;
import com.quectel.core.module.really.dto.ReallyCompanyDto;
import com.quectel.core.module.really.dto.ReallyCompanyUserDto;
import com.quectel.core.module.really.entity.ReallyCompanyUserEntity;
import com.quectel.core.module.really.service.ReallyCompanyService;
import com.quectel.core.module.really.service.ReallyCompanyUserService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-09 14:31:07
 */
@DubboService
public class ReallyCompanyUserServiceImpl implements ReallyCompanyUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReallyCompanyUserServiceImpl.class);

    private static final String REALLY_COMPANY_USER_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "REALLY_COMPANY_USER_CACHE_BY_ID:";

    @Autowired
    private ReallyCompanyUserDao reallyCompanyUserDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private ReallyCompanyService reallyCompanyService;

    @Override
    public ReallyCompanyUserDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                REALLY_COMPANY_USER_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ReallyCompanyUserDto selectById(Long id) {
        ReallyCompanyUserEntity entity = reallyCompanyUserDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ReallyCompanyUserDto.class));
    }

    @Override
    public List<ReallyCompanyUserDto> queryList(Map<String, Object> params) {
        List<ReallyCompanyUserEntity> list = reallyCompanyUserDao.queryList(params);
        List<ReallyCompanyUserDto> result = CopyUtils.copyList(list, ReallyCompanyUserDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ReallyCompanyUserDto paddingField(ReallyCompanyUserDto dto) {
        if (dto != null) {
            if (dto.getVillageId() != null) {
                VillageDto villageDto = villageService.selectById(dto.getVillageId());
                if (villageDto != null) {
                    dto.setVillageName(villageDto.getName());
                }
            }
            if (dto.getReallyCompanyId() != null) {
                ReallyCompanyDto reallyCompanyDto = reallyCompanyService.selectById(dto.getReallyCompanyId());
                if (reallyCompanyDto != null) {
                    dto.setReallyCompanyName(reallyCompanyDto.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return reallyCompanyUserDao.queryTotal(params);
    }

    @Override
    public Long save(ReallyCompanyUserDto dto) {
        ReallyCompanyUserEntity entity = CopyUtils.copyObj(dto, ReallyCompanyUserEntity.class);
        entity.setId(Snowflake.nextId());
        reallyCompanyUserDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ReallyCompanyUserDto dto) {
        ReallyCompanyUserEntity entity = CopyUtils.copyObj(dto, ReallyCompanyUserEntity.class);
        reallyCompanyUserDao.updateById(entity);
        CacheKit.invalidRedisCache(REALLY_COMPANY_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(ReallyCompanyUserDto dto) {

        ReallyCompanyUserEntity entity = CopyUtils.copyObj(dto, ReallyCompanyUserEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, reallyCompanyUserDao);

        CacheKit.invalidRedisCache(REALLY_COMPANY_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        reallyCompanyUserDao.deleteById(id);
        CacheKit.invalidRedisCache(REALLY_COMPANY_USER_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
